<template>
  <div>
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="sip频道管理" name="second">
        <div>
          <el-button type="success" @click="dialog">新增</el-button>
        </div>
        <el-card class="box-card">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column prop="Initiator_id" label="广播编号" width="180">
            </el-table-column>
            <el-table-column prop="Initiator_name" label="广播名称" width="180">
            </el-table-column>
            <el-table-column prop="channel" label="广播频道" width="180">
            </el-table-column>
            <el-table-column fixed="right" label="" width="100">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="dialog(scope.row)"
                  >编辑</el-button
                >
                <el-button type="text" size="small" @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-tab-pane>
    </el-tabs>

    <!-- sip频道管理对话框对话框 -->
    <el-dialog
      title=""
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="广播编号">
          <el-input v-model="form.Initiator_id"></el-input>
        </el-form-item>
        <el-form-item label="广播名称">
          <el-input v-model="form.Initiator_name"></el-input>
        </el-form-item>
        <el-form-item label="广播网关">
          <el-input v-model="form.ip_gateway"></el-input>
        </el-form-item>
         <el-form-item label="广播端口">
          <el-input v-model="form.port"></el-input>
        </el-form-item>
        <el-form-item label="广播类型">
          <el-select v-model="form.channel" placeholder="请选择">
            <el-option value="CH14" label="CH14"></el-option>
            <el-option value="CH16" label="CH16"></el-option>
            <el-option value="CH0" label="CH0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addEdit">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  ajaxInitiator,
  sipInitiatorPut,
  sipInitiatorAdd,
  sipInitiatorDel,
  sipGet,
  sipDispositionUpdate,
} from "@/api/sip";
import { getRouter, getUser, putUser } from "@/api/user";
import store from "@/store/modules/user";
export default {
  created() {
    this.init();
    this.sip_init();
  },
  computed: {},
  data() {
    return {
      userId: "",
      activeName: "second",
      tableData: [],
      dialogVisible: false,
      form: {
        Initiator_id: "",
        Initiator_name: "",
        ip_gateway: "",
        port: "",
        type: "",
      },
      //sip配置数据
      sipForm: {},
    };
  },
  methods: {
    //获取频道表
    async init() {
      let name = JSON.parse(localStorage.getItem("HSKJ-username"));
      let res = await ajaxInitiator({ userChanne: name });
      this.tableData = res.data.meta.data;
    },
    async sip_init() {
      let res = await sipGet();
      this.sipForm = res.data.data[0];
    },
    // 对话框
    dialog(data) {
      console.log(store.state.sip_argument);
      //对象浅拷贝
      if (data.id) {
        let copyData = Object.assign({}, data);

        this.form = copyData;
      }

      this.dialogVisible = true;
    },
    async addEdit() {
      // 编辑请求
      if (this.form.id) {
        let res = await sipInitiatorPut(this.form);
        if (res.data.meta.code === 200) {
          this.$message.success("修改成功");
          this.init();
          this.form = { Initiator_id: "", Initiator_name: "" };
          this.dialogVisible = false;
        }
      } else {
        // 新增请求
        let res = await sipInitiatorAdd(this.form);
        if (res.data.meta.code === 200) {
          this.$message.success("新增成功");
          this.init();
          this.form = { Initiator_id: "", Initiator_name: "" };
          this.dialogVisible = false;
        }
      }
    },
    async del(data) {
      let res = await sipInitiatorDel(JSON.stringify({ id: data.id }));

      if (res.data.meta.code === 200) {
        this.$message.success("删除成功");
        this.init();
      }
    },
    async sipButton() {
      let res = await sipDispositionUpdate(this.sipForm);
      if (res.status === 200) {
        this.$message.success(res.data.meta.msg);
        this.sip_init();
      }
    },
    handleClick(tab, event) {},
    handleClose(done) {
      this.dialogVisible = false;
      this.form = {};
    },
  },
};
</script>

<style scoped="less">
.box {
  display: flex;
  justify-content: center;
}
</style>
